/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.swta;
import de.swta.components.category.CategoryManager;
import de.swta.components.unit.UnitManager;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.RangeValidator;
import org.apache.wicket.validation.validator.StringValidator.LengthBetweenValidator;

/**
 *
 * @author Jan
 */
public final class AddItem extends WebPage {

    private RequiredTextField nameField = new RequiredTextField("name");
    final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
    private RequiredTextField idField = new RequiredTextField("id");
    private RequiredTextField weightField = new RequiredTextField("weight");
    private RequiredTextField priceField = new RequiredTextField("price");


    Form itemForm;

    public AddItem() {
        add(new StyleSheetReference("stylesheet", AddItem.class, "style.css"));
        this.itemForm = new Form("itemForm");

        this.itemForm.setModel(new CompoundPropertyModel(new MyItem()));

        this.itemForm.add(new DropDownChoice("unit", UnitManager.getInstance().getUnitNames()));
        this.itemForm.add(new DropDownChoice("category", CategoryManager.getInstance().getCategoryNames()));
        this.itemForm.add(idField.add(new LengthBetweenValidator(3, 20)));
        this.itemForm.add(nameField.add(new LengthBetweenValidator(3, 20)));
        this.itemForm.add(weightField.add(new RangeValidator(100, 2000)));
        this.itemForm.add(priceField.add(new RangeValidator(1d, 2000d)));
        this.itemForm.add(this.feedbackPanel);

        this.itemForm.add(new SubmitLink("submitLink") {

            @Override
            public void onSubmit() {
                MyItem itemModel = (MyItem) AddItem.this.itemForm.getModelObject();
                MyItemManager.getInstance().addItem(itemModel);

                setResponsePage(MyItems.class);
            }

        });

        Button cancelButton = new Button("cancelButton") {

            @Override
            public void onSubmit() {
                setResponsePage(MyItems.class);
            }

        };

        cancelButton.setDefaultFormProcessing(false);
        this.itemForm.add(cancelButton);
        this.add(this.itemForm);
    }
}

